import {describe} from 'mocha'
import Sinon from 'sinon'
import {assert, expect} from 'chai'
import * as path from 'path'
import * as fs from 'fs'
import * as util from '../src/util'
import {PackInputs} from '../src/pack-inputs'
import * as step from '@ali/flow-step-toolkit';

describe('Archive Files', () => {
    before(() => {
        const testTemp = path.join(__dirname, 'test_temp')
        if (!fs.existsSync(testTemp)) {
            fs.mkdirSync(testTemp)
            fs.writeFileSync(`${testTemp}${util.SEP}a.txt`, 'test')
            fs.writeFileSync(`${testTemp}${util.SEP}b.txt`, 'test')
        }
        const testOutput = path.join(__dirname, 'test_output')
        if (!fs.existsSync(testOutput)) {
            fs.mkdirSync(testOutput)
        }
    })
    after(() => {
        const testTemp = path.join(__dirname, 'test_temp')
        if (fs.existsSync(testTemp)) {
            util.clearDir(testTemp)
            fs.rmdirSync(testTemp)
        }
        const testOutput = path.join(__dirname, 'test_output')
        if (fs.existsSync(testOutput)) {
            util.clearDir(testOutput)
            fs.rmdirSync(testOutput)
        }
    })

    it('Successfully created a tgz when filePath is directory', async () => {
        let expectArchivePath = path.join(__dirname, 'test_output', 'myTgz.tgz')
        let packInputs = {} as PackInputs
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = 'test_temp'
        let s = __dirname + util.SEP + 'test_temp'
        packInputs.sourcePath = [s]
        let res = await util.tarPack(packInputs)
        expect(res).equal(0)
    })

    it('Successfully created a tgz when filePath contains space', async () => {
        let inputFilePath = 'a.txt b.txt'
        let inputs = inputFilePath.split(' ')
        let packInputs = {} as PackInputs
        let arr: string[] = []
        for (let i = 0; i < inputs.length; i++) {
            let s = __dirname + util.SEP + 'test_temp' + util.SEP + inputs[i]
            arr.push(s)
        }
        packInputs.sourcePath = [...arr]
        let expectArchivePath = path.join(__dirname, 'test_output', 'myTgz.tgz')
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = inputFilePath
        let res = await util.tarPack(packInputs)
        expect(res).equal(0)
    })

    it('Successfully created a tgz when filePath is file', async () => {
        let expectArchivePath = path.join(__dirname, 'test_output', 'myTgz.tgz')
        let packInputs = {} as PackInputs
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = path.join('test_temp', 'a.txt')
        let s = __dirname + util.SEP + 'test_temp' + util.SEP + 'a.txt'
        packInputs.sourcePath = [s]
        let res = await util.tarPack(packInputs)
        expect(res).equal(0)
    })

    it('Successfully created a tgz include path', async () => {
        let expectArchivePath = path.join(__dirname, 'test_output', 'myTgz.tgz')
        let packInputs = {} as PackInputs
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = path.join('test_temp', 'a.txt')
        let s = __dirname + util.SEP + 'test_temp' + util.SEP + 'a.txt'
        packInputs.sourcePath = [s]
        packInputs.includePathInArtifact = 'true'
        let res = await util.tarPack(packInputs)
        expect(res).equal(0)
    })

    it('Successfully created a zip when filePath is directory', async () => {
        let expectArchivePath = path.join(__dirname, 'test_output', 'myZip.zip')
        let packInputs = {} as PackInputs
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = path.join('test_temp')
        packInputs.sevenZipLocation = path.join(
            path.resolve(__dirname, '..'),
            `src${util.SEP}binary${util.SEP}7zip${util.SEP}7z.exe`
        )
        let s = __dirname + util.SEP + 'test_temp'
        packInputs.sourcePath = [s]
        let mocker = Sinon.mock(step.exec)
        mocker.expects('call').resolves(0)
        let res = await util.sevenZipPack(packInputs)
        expect(res).equal(0)
        mocker.verify()
        mocker.restore()
    })

    it('Successfully created a zip when filePath is file', async () => {
        let expectArchivePath = path.join(__dirname, 'test_output', 'myZip.zip')
        let packInputs = {} as PackInputs
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = path.join('test_temp', 'a.txt')
        let s = __dirname + util.SEP + 'test_temp' + util.SEP + 'a.txt'
        packInputs.sourcePath = [s]
        packInputs.sevenZipLocation = path.join(
            path.resolve(__dirname, '..'),
            `src${util.SEP}binary${util.SEP}7zip${util.SEP}7z.exe`
        )
        let mocker = Sinon.mock(step.exec)
        mocker.expects('call').resolves(0)
        let res = await util.sevenZipPack(packInputs)
        expect(res).equal(0)
        mocker.verify()
        mocker.restore()
    })

    it('Successfully created a zip include path', async () => {
        let expectArchivePath = path.join(__dirname, 'test_output', 'myZip.zip')
        let packInputs = {} as PackInputs
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = path.join('test_temp', 'a.txt')
        let s = __dirname + util.SEP + 'test_temp' + util.SEP + 'a.txt'
        packInputs.sourcePath = [s]
        packInputs.includePathInArtifact = 'true'
        packInputs.sevenZipLocation = path.join(
            path.resolve(__dirname, '..'),
            `src${util.SEP}binary${util.SEP}7zip${util.SEP}7z.exe`
        )
        let mocker = Sinon.mock(step.exec)
        mocker.expects('call').resolves(0)
        let res = await util.sevenZipPack(packInputs)
        expect(res).equal(0)
        mocker.verify()
        mocker.restore()
    })

    it('Successfully created a zip when filePath contains space', async () => {
        let inputFilePath = 'a.txt b.txt'
        let inputs = inputFilePath.split(' ')
        let packInputs = {} as PackInputs
        let arr: string[] = []
        for (let i = 0; i < inputs.length; i++) {
            let s = __dirname + util.SEP + 'test_temp' + util.SEP + inputs[i]
            arr.push(s)
        }
        packInputs.sourcePath = [...arr]
        let expectArchivePath = path.join(__dirname, 'test_output', 'myZip.zip')
        packInputs.artifactFullName = expectArchivePath
        packInputs.filePath = inputFilePath
        packInputs.sevenZipLocation = path.join(
            path.resolve(__dirname, '..'),
            `src${util.SEP}binary${util.SEP}7zip${util.SEP}7z.exe`
        )
        let mocker = Sinon.mock(step.exec)
        mocker.expects('call').resolves(0)
        let res = await util.tarPack(packInputs)
        expect(res).equal(0)
        mocker.verify()
        mocker.restore()
    })
})
